package cn.jiangjiesheng.bootstrap.commons.utils;

import com.google.common.base.Joiner;
import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pengzhiyuan on 2020/9/11
 */
public class PTableConditionUtils {

    private static final String SORT_DEFAULT_FIELD = "id";

    private static final String SORT_DESC = "desc";

    private static final String SORT_ASC = "asc";

    /**
     * 设置分页参数,不设置排序
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public static Map<String, Object> setPageInfo(Integer pageNum, Integer pageSize) {
        if (pageNum == null && pageSize == null) {
            return null;
        }
        Map<String, Object> expendCondition = new HashMap<>();
        Map<String, Object> pageMap = Maps.newHashMap();
        if (pageNum < 1) {
            pageNum = 1;
        }
        pageMap.put("start", (pageNum - 1) * pageSize);
        pageMap.put("perPage", pageSize);
        expendCondition.put("paging", pageMap);
        return expendCondition;
    }

    /**
     * 设置分页参数,指定字段排序
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public static Map<String, Object> setPageInfo(Integer pageNum, Integer pageSize, String sortColumn, String sortType) {
        if (pageNum == null && pageSize == null) {
            return null;
        }
        Map<String, Object> expendCondition = new HashMap<>();
        Map<String, Object> pageMap = Maps.newHashMap();
        if (pageNum < 1) {
            pageNum = 1;
        }
        pageMap.put("start", (pageNum - 1) * pageSize);
        pageMap.put("perPage", pageSize);
        expendCondition.put("paging", pageMap);
        Map<String, Object> map = new HashMap<>();
        map.put("field", sortColumn);
        map.put("type", sortType);
        expendCondition.put("order", map);
        return expendCondition;
    }

    /**
     * 设置分页参数,可以设置按id倒排序
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public static Map<String,Object> setPageInfo(Integer pageNum, Integer pageSize, Boolean sortById) {
        Map<String, Object> expendCondition = new HashMap<>();
        if (sortById) {
            expendCondition.put("order", sort(SORT_DEFAULT_FIELD).get("order"));
        }
        expendCondition.put("paging", setPageInfo(pageNum, pageSize).get("paging"));
        return expendCondition;
    }


    /**
     * 查询数据排序
     */
    public static Map<String, Object> sort() {
        return sort(SORT_DEFAULT_FIELD, SORT_DESC);
    }

    public static Map<String, Object> sort(String field) {
        return sort(field, SORT_DESC);
    }

    public static Map<String, Object> sort(String field, String sortType) {
        Map<String, Object> expendCondition = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("field", field);
        map.put("type", sortType);
        expendCondition.put("order", map);
        return expendCondition;
    }

    public static Map<String, Object> group(List<String> field) {
        Map<String, Object> expendCondition = new HashMap<>();
        String groupStr = Joiner.on(",").join(field);
        expendCondition.put("group", groupStr);
        return expendCondition;
    }

}
